const Logic = require('../logic');
const Validater = require('fastest-validator');
module.exports = class extends Logic
{

    constructor(ctx){
        super(ctx);
        this.V=new Validater();
    }
    /**
     * put resource
     * @return {Promise} []
     */
    async postAction()
    {
        let data = this.post('data');
        if (think.isEmpty(data))
        {
            return this.fail(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }

        try
        {
            data = JSON.parse(data);
        } catch (e)
        {
            return this.fail(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }
        const cpguid = data.cpguid;
        if (think.isNullOrUndefined(cpguid))
        {
            return this.fail(52022, 'cpguid不存在,请检查cpguid');
        }

        let applyType = await this.model('apiv1/competition').where({PK_CompetitionGuid: cpguid}).getField('ApplyType', 1);


        if (applyType[0] === '团队')
        {
            //todo 验证团队数据

            let teamRule={
                TeamName: { type: "string", min:1},
                Menmber: { type: "array",   },
                OrgName: { type: "string",  min:1 },
                Name: { type: "string",  min:1 },
                Tel: { type: "string",   min:5,max:11},
                Email: { type: "email",   },
            };
            let vres=this.V.validate(data.team,teamRule);
            if(vres===true){
                this.ctx.state.teamData = data.team;
            }else{
                return this.error(52030,vres[0].message);
            }
        } else if (applyType[0] === '个人')
        {
            //todo 验证个人数据
            let psrRule={
                Name: { type: "string", min:1},
                OrgName: { type: "string", min:1},
                Tel: { type: "string", min:1,max:11},
                Email: { type: "email"},
            };
            let vres=this.V.validate(data.team,psrRule);
            if(vres===true){
                this.ctx.state.psrData = data.psr;
            }else{
                return this.error(52030,vres[0].message);
            }

        } else
        {
            return this.fail(52023, 'cpguid或比赛不存在,请检查cpguid');
        }

    }


    uploadWorkAction(){

        let data=null;
        try
        {
            data = JSON.parse(this.post('data'));
        } catch (e)
        {
            return this.fail(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }
        let upworkRule={
            stepguid: { type: "string", min:36,max:36},
            CoverUrl: { type: "string", min:5},
            Title: { type: "string", min:1},
            Desc: { type: "string",min:1},
            Content: { type: "string",min:10},
        };
        let vres=this.V.validate(data,upworkRule);
        if(vres===true){
            this.ctx.state.uploadWorkData = data;
        }else{
            return this.error(52040,vres[0].message);
        }
    }
};
